package servlet;

import dao.Db;
import javabean.Worker;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/add")
public class AddData extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //收参
        String name = req.getParameter("name");
        String work = req.getParameter("work");

        Worker worker = new Worker(null,name,work);
        System.out.println("worker = " + worker);
        //访问数据访问层
        Db db = new Db();
        int i=db.addData(worker);
        System.out.println("i = " + i);
        if (i>0){
            req.getRequestDispatcher("worker").forward(req,resp);
        }else{
            System.out.println("删除失败");
        }
    }
}
